package com.nowcoder.code.hjnc;

import com.algorithm.model.ListNode;
import com.algorithm.model.ListNodeService;

public class NC3 {

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        ListNode fast = pHead;
        ListNode slow = pHead;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        fast = pHead;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public static void main(String[] args) {
        int[] is = {1, 2};
        new NC3().EntryNodeOfLoop(ListNodeService.getByArray(is));

    }

}
